<?php
/**
 * Backup & Restore class
 *
 * Use this class for doing Backup & Restore in MySQLcompare
 *
 * PHP version 5
 *
 * Copyright (C) 2012 Ewald van Gemert (ewald@vangee.nl)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @category   Development tool
 * @package    MySQLcompare
 * @author     Ewald van Gemert <ewald@vangee.nl>
 * @license    MIT 3
 * @version    SVN: 
 * @link       http://code.google.com/p/mysqlcompare
 * @since      File available since Release 0.9
 */

// ###########################################################################
// Class Backup & Restore
// ###########################################################################
class Backuprestore {
	private $server = "";
	private $db = "";
	private $setting = array();
	private $obj = array();
	private $srvitem = null;

	/* Backup this db */	
	private function bck() {	
		$bckfile = $this->setting['backupfolder'].'/'.$this->db;
		if (!file_exists($bckfile)) mkdir($bckfile, 0700, true);
		$file = '/backup_'.date('Y-m-d_H.i.s').'.sql';
		$bckfile .= $file;
		$this->srvitem->backup($this->db, $bckfile);
		$this->obj['backup']['status'] = 'Backup OK ('.$file.')';
		return $this->obj;
	}

	/* Drop all `restore` options for this db */
	private function drp() {
		delTree($this->setting['backupfolder'].'/'.$this->db);
		$this->obj['backup']['status'] = 'Backup files dropped';
		return $this->obj;
	}
	
	/* Get a list of all `restore` options for this db */
	private function lst() {
		$msg = $lst = "";
		if (file_exists($this->setting['backupfolder'].'/'.$this->db)) {
		    $this->obj['backup']['files'] = array();
			$arr = array_diff(scandir($this->setting['backupfolder'].'/'.$this->db), array('.','..'));
			$this->obj['backup']['files'] = $arr;
			foreach ($arr as $key => $value) {
				//$msg .= "[{$key}] : {$value}\n";
				$size = (int)(filesize($this->setting['backupfolder'].'/'.$this->db.'/'.$value)/1024);
				$name = str_replace(array(".sql","_","."), array(""," ",":"), $value);
				$lst .= "<li><a href=\"db={$this->server}.{$this->db}&action=cmdres&id=".urlencode($value)."\" class=\"dbrestore\">{$name}</a> {$size}kB</li>";
			}
			//$this->obj['backup']['list'] = $msg;
			$this->obj['backup']['htmllist'] = $lst;
		}
		$this->obj['backup']['status'] = 'List';
		return $this->obj;
	}
	
	/* Restore selected restore file into DB */
	private function res($id) {
		$file = $this->setting['backupfolder'].'/'.$this->db.'/'.$id;
		$arr = array_diff(scandir($this->setting['backupfolder'].'/'.$this->db), array('.','..'));
		if (in_array($id,$arr)) {
			$this->srvitem->restore($this->db, $file);
			$this->obj['backup']['file'] = $file;
			$this->obj['backup']['status'] = 'No errors occurd, succes of restore action is not verified.';
		} else {
			$this->obj['backup']['status'] = "Could not find backup file ({$file}) to restore ({$this->db}).";
		}
		return $this->obj;
	}
	
	// ***********************************************************************
	// public functions
	// ***********************************************************************
	public function __construct($srv, $setting, $db) {
		list($this->server, $this->db) = explode(".", $db."..");
		$this->srvitem = $srv->si($this->server);
		$this->obj['backup'] = array();
		$this->setting = $setting;
	}

	public function execute($action, $file=null) {
		switch ($action) {
			case 'bck':
				return $this->bck();
				break;
			case 'drp':
				return $this->drp();
				break;
			case 'res':
				return $this->res($file);
				break;
			default:
				return $this->lst();
		}
	}

}